#include <winsock2.h>
#include <iostream>

/*
h: 主机字节序
n: 网络字节序
s: 短整型
l: 长整型

主要用于ip和端口的转换
*/

using namespace std;
int main() {
    // 初始化Winsock
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        cerr << "WSAStartup failed" << endl;
        return 1;
    }

    // 不常用

    // 将一个短整型从主机字节序转换位网络字节序
    unsigned short var1_host = 0x1234;
    unsigned short var1_net = htons(var1_host);

    printf("var1_host : %04x \n", var1_host);
    printf("var1_net: %04x \n", var1_net);

    // 将一个整型从主机字节序转换位网络字节序
    uint32_t var2_host = 0x12345678;
    uint32_t var2_net = htonl(var2_host);

    printf("var2_host : %08x \n", var2_host);
    printf("var2_net: %08x \n", var2_net);

    // 将一个短整型从网络字节序转换位主机字节序
    uint16_t var3_net = 0x1234;
    uint16_t var3_host = ntohs(var3_net);

    printf("var3_net: %04x \n", var3_net);
    printf("var3_host : %04x \n", var3_host);

    // 将一个整型从网络字节序转换位主机字节序
    uint32_t var4_net = 0x12345678;
    uint32_t var4_host = ntohl(var4_net);
    
    printf("var4_net: %08x \n", var4_net);
    printf("var4_host: %08x \n", var4_host);

    WSACleanup();
    return 0;
}
